#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>

using std::cout;
using std::endl;
using std::cin;
using std::istream;
using std::ostream;
using std::vector;
using std::string;


class Singleton
{
    class AutoRelease
    {
    public:
        AutoRelease(){};
        ~AutoRelease();
    };

public:     // 公有成员函数
    static Singleton* getInstance();
    void show(void) const;
private:    // 私有成员函数
    Singleton(const char* name);
    ~Singleton();
    static void destroy();
private:    // 私有数据成员
    char* _name;
    static Singleton* _pinstance;
    static AutoRelease _aaa;
};
Singleton* Singleton::_pinstance = nullptr;
Singleton::AutoRelease Singleton::_aaa;


Singleton* Singleton::getInstance(){
    if(!_pinstance){
        _pinstance = new Singleton ("Huzhu is the best boy!");
    }
    return _pinstance;
}

void Singleton::destroy(){
    if(_pinstance){
        delete _pinstance;
        _pinstance = nullptr;
    }
}

Singleton::Singleton(const char* name)
    : _name(new char[strlen(name) + 1] ()){
    strcpy(_name, name);
}

Singleton::~Singleton(){
    if(_name){
        delete [] _name;
        _name = nullptr;
    }
}

void Singleton::show(void) const{
    cout << _name << endl;
}

////////////////////////////////////////////////////////
Singleton::AutoRelease::~AutoRelease(){
    destroy();
}

// 使用嵌套类和静态对象的方式，实现单例模式的自动释放
int main(void)
{
    Singleton::getInstance()->show();
    Singleton::getInstance()->show();
    Singleton::getInstance()->show();

    return 0;
}

